package registration.adventurezone.thevine.com;

import java.util.ArrayList;
import java.util.Arrays;

import registration.adventurezone.thevine.com.Constants.ChildrenFilter;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewFlipper;

public class DatabaseActivity extends Activity{

    private ListView childrenListView;
    EditText filterBox;
    private ArrayList<ChildViewHolder> childrenList;
    private ChildViewHolder[] childViews;
    private ArrayAdapter<ChildViewHolder> listAdapter;
    private View databaseListLayout, databaseSingleLayout;
    private DBChild selectedChild;
    private ViewFlipper flipper;
    private Animation animInRight, animOutLeft, animOutRight, animInLeft;
    
    private TextView childNameField, childGenderField, childDOBField, 
    		childAgeField, childAllergiesField, parentNameField, phoneField, 
    		addressField, cityField, zipField, churchPhoneField, churchPhonePrefField;
    
    private EditText childFirst, childLast, month, day, year, childAllergies,
    		parentFirst, parentLast, phone, address, city, zip, churchPhone;
    private GridView gridView;
    private Button editChildNameButton, editGenderButton, editDOBButton, 
    				editAllergiesButton, editParentNameButton,
    				editPhoneButton, editAddressButton,
    				editCityButton,	editZipButton,
    				editChurchPhoneButton, editPhonePrefButton;


	@Override
    public void onCreate( Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.database_layout);

        loadResourcesById();
        childrenListView = (ListView) findViewById(R.id.childrenList);

        ArrayList<DBChild> children = XMLResourceHandler.getInstance().getChildrenByStatus(ChildrenFilter.ALL);
    	childViews = new ChildViewHolder[children.size()];
    	for (int i=0;i<children.size();i++) {
    		childViews[i] = new ChildViewHolder(children.get(i));
    	}

    	childrenList = new ArrayList<ChildViewHolder>();
        childrenList.addAll(Arrays.asList(childViews));
        listAdapter = new ChildArrayAdapter(this, childrenList, R.layout.simple_list_row);
        childrenListView.setAdapter(listAdapter);
        

        filterBox = (EditText) findViewById(R.id.childrenFilter);


        // By using setTextFilterEnabled method in listview we can filter the listview items.

        childrenListView.setTextFilterEnabled(true);
        
        filterBox.addTextChangedListener(new TextWatcher()
        {


            @Override
            public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
            {
                // TODO Auto-generated method stub

            }



            @Override
            public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
            {
                // TODO Auto-generated method stub

            }



            @Override
            public void afterTextChanged( Editable arg0)
            {
                DatabaseActivity.this.listAdapter.getFilter().filter(arg0);
            }
        });
        
        childrenListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	            imm.hideSoftInputFromWindow(filterBox.getApplicationWindowToken(), 0);
	            
	            
	            // Get data for selected child
	            selectedChild = listAdapter.getItem(position).getDBChild();
				ArrayList<DBChild> toInspect = new ArrayList<DBChild>();
				toInspect.add(selectedChild);
				ArrayList<DBChild> retList = XMLResourceHandler.getInstance().fullInspectSelectedChildren(toInspect);
	            selectedChild = retList.get(0);
	            
	            // Set next fields for next screen
				childNameField.setText(selectedChild.getChildFirst() + " " + selectedChild.getChildLast());
		    	childGenderField.setText(selectedChild.getGender());
		    	int [] dob = selectedChild.getDob();
		    	String temp = dob[0]+ "/" +dob[1] + "/" + dob[2];
		    	childDOBField.setText(temp);
		    	childAgeField.setText(String.valueOf(selectedChild.getAge()));
		    	childAllergiesField.setText(selectedChild.getAllergies());
		    	parentNameField.setText(selectedChild.getParentFirst() + " " + selectedChild.getParentLast());
		    	phoneField.setText(StringUtils.parsePhoneNumber(selectedChild.getPhone()));
		    	addressField.setText(selectedChild.getAddress());
		    	cityField.setText(selectedChild.getCity());
		    	zipField.setText(selectedChild.getZip());
		    	churchPhoneField.setText(StringUtils.parsePhoneNumber(selectedChild.getChurchPhone()));
		    	churchPhonePrefField.setText(selectedChild.getChurchPref());
	            
		    	
		    	gridView.setAdapter(new CellAdapter(getBaseContext(), selectedChild.getGuardians()));
		    	
	            // Slide to next screen
				flipper.setInAnimation(animInRight);
			    flipper.setOutAnimation(animOutLeft);
			    flipper.showNext();  
			    
			    
		       
			}
		});
        
        editChildNameButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_name_dialog);
    			dialog.setTitle("Edit Child Name");
     
    			childFirst = (EditText) dialog.findViewById(R.id.first);
    			childFirst.setText(selectedChild.getChildFirst());
    			childLast = (EditText) dialog.findViewById(R.id.last);
    			childLast.setText(selectedChild.getChildLast());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					selectedChild.setChildFirst(childFirst.getText().toString());
    					selectedChild.setChildLast(childLast.getText().toString());
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editGenderButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_gender_dialog);
    			dialog.setTitle("Edit Child Gender");
     
    			RadioGroup genderRadioGroup = (RadioGroup) findViewById(R.id.radioGenderDialog);
    			RadioButton genderRadioButton1 = (RadioButton) findViewById(R.id.radioMaleDialog);
    			RadioButton genderRadioButton2 = (RadioButton) findViewById(R.id.radioFemaleDialog);
    			
    			//int selectedId = genderRadioGroup.getCheckedRadioButtonId();
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editDOBButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_dob_dialog);
    			dialog.setTitle("Edit Child DOB");
     
    			// set the custom dialog components - text, image and button
    			month = (EditText) dialog.findViewById(R.id.month);
    			month.setText(String.valueOf(selectedChild.getDob()[0]));
    			day = (EditText) dialog.findViewById(R.id.day);
    			day.setText(String.valueOf(selectedChild.getDob()[1]));
    			year = (EditText) dialog.findViewById(R.id.year);
    			year.setText(String.valueOf(selectedChild.getDob()[2]));
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editAllergiesButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_allergies_dialog);
    			dialog.setTitle("Edit Child Allergies");
     
    			// set the custom dialog components - text, image and button
    			childAllergies = (EditText) dialog.findViewById(R.id.allergies);
    			childAllergies.setText(selectedChild.getAllergies());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editParentNameButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_name_dialog);
    			dialog.setTitle("Edit Parent Name");
     
    			// set the custom dialog components - text, image and button
    			parentFirst = (EditText) dialog.findViewById(R.id.first);
    			parentLast = (EditText) dialog.findViewById(R.id.last);
    			parentFirst.setText(selectedChild.getParentFirst());
    			parentLast.setText(selectedChild.getParentLast());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editPhoneButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_phone_dialog);
    			dialog.setTitle("Edit Phone Number");
     
    			// set the custom dialog components - text, image and button
    			phone = (EditText) dialog.findViewById(R.id.phone);
    			phone.setText(selectedChild.getPhone());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editAddressButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_address_dialog);
    			dialog.setTitle("Edit Address");
     
    			// set the custom dialog components - text, image and button
    			address = (EditText) dialog.findViewById(R.id.address);
    			address.setText(selectedChild.getAddress());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editCityButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_city_dialog);
    			dialog.setTitle("Edit City");
     
    			// set the custom dialog components - text, image and button
    			city = (EditText) dialog.findViewById(R.id.city);
    			city.setText(selectedChild.getCity());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editZipButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_zip_dialog);
    			dialog.setTitle("Edit Zip");
     
    			// set the custom dialog components - text, image and button
    			zip = (EditText) dialog.findViewById(R.id.zip);
    			zip.setText(selectedChild.getZip());
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editChurchPhoneButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_phone_dialog);
    			dialog.setTitle("Edit Church Phone");
     
    			// set the custom dialog components - text, image and button
    			churchPhone = (EditText) dialog.findViewById(R.id.phone);
    			churchPhone.setText(selectedChild.getChurchPhone());
    			
    			
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});
        editPhonePrefButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		final Dialog dialog = new Dialog(v.getRootView().getContext());
    			dialog.setContentView(R.layout.edit_contact_method_dialog);
    			dialog.setTitle("Edit Contact Method");
     
    			RadioGroup churchPhonePrefRadioGroup = (RadioGroup) findViewById(R.id.radioChurchPhonePrefDialog);
    			RadioButton churchPhonePrefRadioButton1 = (RadioButton) findViewById(R.id.radioCallDialog);
    			RadioButton churchPhonePrefRadioButton2 = (RadioButton) findViewById(R.id.radioTextDialog);
    			/*
    			if(selectedChild.getChurchPref().equals("Call")) {
    				churchPhonePrefRadioButton1.setChecked(true);
    			}
    			else {
    				churchPhonePrefRadioButton2.setChecked(true);
    			}
    			*/
    			Button dialogButton = (Button) dialog.findViewById(R.id.okButton);
    			Button dialogButton2 = (Button) dialog.findViewById(R.id.cancelButton);
    			// if button is clicked, close the custom dialog
    			dialogButton.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					//TODO: Set Changes to Child object
    					XMLResourceHandler.getInstance().updateChildData(selectedChild);
    					XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
    					dialog.dismiss();
    				}
    			});
    			dialogButton2.setOnClickListener(new OnClickListener() {
    				@Override
    				public void onClick(View v) {
    					dialog.dismiss();
    				}
    			});
     
    			dialog.show();
        	}
		});

    }
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	// Are we on the second layout view?
	    	if(flipper.getDisplayedChild() == 1){
	    		// go back to first view
		    	flipper.setInAnimation(animInLeft);
			    flipper.setOutAnimation(animOutRight);
			    flipper.showPrevious(); 
			    return true;
	    	}
	    	// We must be on the first layout view.
	    	else{
	    		//step out of current activity
	    		onBackPressed();
	    		
	    	}
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
    private void loadResourcesById() {
    	databaseListLayout = (View) findViewById(R.id.databaseListLayout);
    	databaseSingleLayout = (View) findViewById(R.id.dataLayout);
    	flipper = (ViewFlipper) findViewById(R.id.databaseFlipper);
    	gridView = (GridView) findViewById(R.id.gridView);
    	animInRight = AnimationUtils.loadAnimation(this, R.anim.view_trans_in_right);
    	animOutLeft = AnimationUtils.loadAnimation(this, R.anim.view_trans_out_left);
    	animOutRight = AnimationUtils.loadAnimation(this, R.anim.view_trans_out_right);
    	animInLeft = AnimationUtils.loadAnimation(this, R.anim.view_trans_in_left);
    	
    	//So many TextViews!
    	childNameField = (TextView) findViewById(R.id.childName);
    	childGenderField = (TextView) findViewById(R.id.childGender);
    	childDOBField = (TextView) findViewById(R.id.childDOB);
    	childAgeField = (TextView) findViewById(R.id.childAge);
    	childAllergiesField = (TextView) findViewById(R.id.childAllergies);
    	parentNameField = (TextView) findViewById(R.id.parentName);
    	phoneField = (TextView) findViewById(R.id.phone);
    	addressField = (TextView) findViewById(R.id.address);
    	cityField = (TextView) findViewById(R.id.city);
    	zipField = (TextView) findViewById(R.id.zip);
    	churchPhoneField = (TextView) findViewById(R.id.churchPhone);
    	churchPhonePrefField = (TextView) findViewById(R.id.churchPhonePref);
    	
    	editChildNameButton = (Button) findViewById(R.id.editChildNameButton);
    	editGenderButton = (Button) findViewById(R.id.editGenderButton);
    	editDOBButton = (Button) findViewById(R.id.editDOBButton);
    	editAllergiesButton = (Button) findViewById(R.id.editAllergiesButton);
    	editParentNameButton = (Button) findViewById(R.id.editParentNameButton);
    	editPhoneButton = (Button) findViewById(R.id.editPhoneButton);
    	editAddressButton = (Button) findViewById(R.id.editAddressButton);
    	editCityButton = (Button) findViewById(R.id.editCityButton);
    	editZipButton = (Button) findViewById(R.id.editZipButton);
    	editChurchPhoneButton = (Button) findViewById(R.id.editChurchPhoneButton);
    	editPhonePrefButton = (Button) findViewById(R.id.editPhonePrefButton);
    }
    
    private class CellAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<Guardian> list;
        private LayoutInflater inflater;

        public CellAdapter(Context c, ArrayList<Guardian> list) {
        	//super(c, R.layout.simple_list_row, R.id.itemTextView, childrenList);
    		//inflater = LayoutInflater.from(c);
            mContext = c;
            this.list = list;
            inflater = LayoutInflater.from(mContext);

        }

        public int getCount() {
            return list.size();
        }

        public Object getItem(int position) {
            return list.get(position);
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView cellImage;
            TextView cellText;
            Guardian g = (Guardian) this.getItem(position);
 
            if (convertView == null) {  // if it's not recycled, initialize some attributes
            	convertView = inflater.inflate(R.layout.grid_cell, null);
    			
            }
            cellImage = (ImageView) convertView.findViewById(R.id.cellImage);
			cellText = (TextView) convertView.findViewById(R.id.cellText);
			
			//Optimization: Tag the row with it's child views, so we don't have to
			//call findViewById() later when we reuse the row.
			//convertView.setTag(new ChildViewHolder(textView, moreIcon));
        	
			//cellImage.setLayoutParams(new GridView.LayoutParams(85, 85));
			cellImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
			cellImage.setPadding(8, 8, 8, 8);
			cellImage.setImageResource(R.drawable.prof_pic);
			
			cellText.setText(g.getFirst() + " " +g.getLast());
            //imageView.setImageResource(mThumbIds[position]);
            return convertView;
        }

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
    }
}
